<script setup lang="ts">
import { Delete, Edit, InfoFilled } from "@element-plus/icons-vue";
import { formattedDate } from "@/utils/formatDate.ts";
import { deleteAdminAPI, getAdminAPI } from "@/api/destribute.ts";
import { onMounted, ref } from "vue";
import { ElMessage } from "element-plus";
import Dialog from "@/view/Distribute/Administrator/components/dialog.vue";

const dialog = ref()
// 获取管理员列表
const adminList = ref()
const getAdminList = async () => {
  const res = await getAdminAPI()
  if (res.code === 0) {
    adminList.value = res.data
  }
}

// 添加管理员
const addAdmin = async () => {
  dialog.value.init({}, 0)
}

// 编辑管理员
const EditAdmin = (row: any) => {
  dialog.value.init(row, 1)
}

// 删除管理员
const deleteAdmin = async (id: string) => {
  const res = await deleteAdminAPI(id)
  if (res.code === 0) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    await getAdminList()
  }
}

onMounted(() => {
  getAdminList()
})
</script>

<template>
  <el-card class="box-card">
    <div class="card-coach-title">
      <el-button type="primary" @click="addAdmin">添加管理员</el-button>
    </div>
    <div class="card-coach-table">
      <el-table
        border
        class="el-table-box"
        ref="multipleTableRef"
        :data="adminList"
        style="width: 100%"
      >
        <el-table-column label="#" width="60" align="center" type="index"/>
        <el-table-column prop="name" align="center" label="名称" width="150"/>
        <el-table-column prop="phone" align="center" label="手机号" width="200"/>
        <el-table-column align="center" label="角色" width="200">
          <template #default="{row}">
            {{ row.role.name }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="创建时间" width="200">
          <template #default="{row}">
            {{ formattedDate(row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="控制">
          <template #default="{row}">
            <div class="operator-btn">
              <el-button type="primary" size="default" :icon="Edit as any" @click="EditAdmin(row)" circle/>
              <el-popconfirm
                width="200"
                @confirm="deleteAdmin(row._id)"
                confirm-button-text="确定"
                cancel-button-text="取消"
                :icon="InfoFilled as any"
                icon-color="#f36b6c"
                title="您确定删除此管理员吗?"
              >
                <template #reference>
                  <el-button type="danger" size="default" :icon="Delete as any" circle/>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <Dialog ref="dialog" @getAdminList="getAdminList"/>
  </el-card>
</template>

<style scoped lang="scss">
.box-card {
  padding: 0 50px;

  .card-coach-title {
    height: 35px;
    display: flex;
    justify-content: flex-end;
  }

  .card-coach-table {
    margin-top: 20px;

    .el-table-box {
      height: calc(90vh - 140px);
    }
  }
}
</style>
